{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.2 64-bit",
   "metadata": {
    "interpreter": {
     "hash": "1b69bd087aa8abd614583c53268aa6b11c2c1c046d6f6b67ad1f1c711f8a952b"
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import sklearn\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "F_FILE_PATH = \"f_train.csv\"\n",
    "SYS_ENCODING = \"ANSI\"\n",
    "df = pd.read_csv(F_FILE_PATH,encoding = SYS_ENCODING)\n",
    "fill_dict = {}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mean_list = ['RBP4','孕前体重','孕前BMI','糖筛孕周','VAR00007','wbc','Cr','BUN','CHO','TG','HDLC','LDLC','ApoA1','APoB','Lpa','hsCRP']\n",
    "cols = list(df.columns.values)\n",
    "before_mean = df.mean(axis = 0, skipna = True)\n",
    "for i in range(1,len(cols) - 1):\n",
    "    col = cols[i]\n",
    "    if(col in mean_list):\n",
    "        fill_dict[col] = before_mean[i]\n",
    "    elif(col == 'BMI分类'):\n",
    "        fill_dict[col] = 0\n",
    "    else:\n",
    "        fill_dict[col] = round(before_mean[i])\n",
    "# print(fill_dict)\n",
    "df.fillna(fill_dict,inplace = True)\n",
    "df.to_csv(\"f_after_wash.csv\",encoding = SYS_ENCODING,index = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('f_after_wash.csv',encoding = SYS_ENCODING)\n",
    "df = df.iloc[:,1:]\n",
    "cols = list(df.columns.values)\n",
    "cols.remove(\"label\")\n",
    "# print(cols)\n",
    "train_size = 700\n",
    "test_size = 1000 - train_size\n",
    "X = df[cols]\n",
    "Y = df[\"label\"]\n",
    "X_train = X[:train_size]\n",
    "X_test = X[train_size:len(X)]\n",
    "Y_train = Y[:train_size]\n",
    "Y_test = Y[train_size:len(Y)]\n",
    "# print(Y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "0.6764705882352942 0.5149253731343284 0.5847457627118645 0.6733333333333333\n"
     ]
    }
   ],
   "source": [
    "from sklearn import datasets\n",
    "from sklearn.linear_model import LinearRegression\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.tree import DecisionTreeClassifier as DTC,export_graphviz\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "# model = DTC(criterion=\"gini\",max_depth=9)\n",
    "model = RandomForestClassifier(n_estimators=150)\n",
    "model.fit(X_train,Y_train)\n",
    "Y_predict = model.predict(X_test)\n",
    "hit_positive = 0\n",
    "predict_positive = 0\n",
    "total_positive = 0\n",
    "hit_total = 0\n",
    "for i in range(test_size):\n",
    "    Y_predict[i] = round(Y_predict[i])\n",
    "    if(Y[i+train_size] == 1):\n",
    "        total_positive = total_positive + 1\n",
    "        if(Y_predict[i] == Y[i+train_size]):\n",
    "            hit_positive = hit_positive + 1\n",
    "    # if(Y_predict[i] == Y[i+train_size]):\n",
    "    #     hit_total = hit_total + 1\n",
    "    if(Y_predict[i] == 1):\n",
    "        predict_positive = predict_positive + 1\n",
    "P = hit_positive / predict_positive\n",
    "R = hit_positive / total_positive\n",
    "F1 = (2 * P * R) / (P + R)\n",
    "print(P,R,F1,model.score(X_test,Y_test))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}